package com.redis.springtest;

import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author Administrator
 */
public class RedisMessageListener implements MessageListener {

    private RedisTemplate redisTemplate;

    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 获取消息
        byte[] body = message.getBody();
        // 使用值序列化器转换
        String msgBody = (String) redisTemplate.getValueSerializer().deserialize(body);
        System.out.println(msgBody);
        // 获取channel
        byte[] channel = message.getChannel();
        // 使用字符串序列化器转换
        String channelStr = (String) redisTemplate.getStringSerializer().deserialize(channel);
        System.out.println(channelStr);
        // channel名称转换
        String bytesStr = new String(pattern);
        System.out.println(bytesStr);
    }
}
